//
//  YZLMyViewController.m
//  Car
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 岳明. All rights reserved.
//

#import "YZLMyViewController.h"
#import "MyHeadView.h"
#import "MyCollectionCell.h"
#import "YZLDiscuss.h"
#import <SDImageCache.h>

#import "YZLMyCollectionViewController.h"

#import "ForthViewController.h"
#import "RentCarViewController.h"
#import "MessageViewController.h"
#import "AboutUsViewController.h"
#import "GrowingTextViewExampleViewController.h"


#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface YZLMyViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, strong) MyHeadView *head;

@property (nonatomic, strong) UICollectionView *collection;

@end

@implementation YZLMyViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)creatHead {
    self.head = [[MyHeadView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/3)];
    [self.view addSubview:self.head];
}

- (void)creatCollection {
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.minimumInteritemSpacing = 0;
    flow.minimumLineSpacing = 0;
    flow.itemSize = CGSizeMake(WIDTH/3, HEIGHT/6);
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, HEIGHT/3, WIDTH, HEIGHT/3) collectionViewLayout:flow];
    [self.view addSubview:self.collection];
    self.collection.backgroundColor = [UIColor whiteColor];
    self.collection.scrollEnabled = NO;
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.collection registerClass:[MyCollectionCell class] forCellWithReuseIdentifier:@"reuse"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 6;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (0 == indexPath.item) {
   
        YZLMyCollectionViewController *vc = [[YZLMyCollectionViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        
    } else if (1 == indexPath.item) {

        
        //查找缓存文件夹
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, 1, YES);
        NSString *sandBoxPath = sandBox[0];
        CGFloat cacheSize = [self folderSizeAtPath:sandBoxPath];
        
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"缓存%.2fMB,是否清除?", cacheSize] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"清除", nil];
        [alert show];
        
    } else if (2 == indexPath.item) {
        AboutUsViewController *about = [[AboutUsViewController alloc] init];
        [self.navigationController pushViewController:about animated:YES];
    }
    else if (3 == indexPath.item) {
        ForthViewController *fourth = [[ForthViewController alloc] init];
        [self.navigationController pushViewController:fourth animated:YES];
    }
    else if (4 == indexPath.item) {
        RentCarViewController *rent = [[RentCarViewController alloc] init];
        [self.navigationController pushViewController:rent animated:YES];
    }
    else if (5 == indexPath.item) {
//        MessageViewController *message = [[MessageViewController alloc] init];
        GrowingTextViewExampleViewController *message = [[GrowingTextViewExampleViewController alloc] init];
         [self.navigationController pushViewController:message animated:YES];
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark - 清除缓存
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
        NSFileManager *fileManager = [NSFileManager defaultManager];
        [fileManager removeItemAtPath:path error:nil];
    }
}

#pragma mark - 计算缓存文件大小
- (long long) fileSizeAtPath:(NSString*) filePath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:filePath]){
        return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];
    }
    
    return 0;
}

- (float) folderSizeAtPath:(NSString*) folderPath{
    NSFileManager* manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:folderPath]) {
        
        return 0;
    }
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];//从前向后枚举器
    NSString* fileName;
    long long folderSize = 0;
    while ((fileName = [childFilesEnumerator nextObject]) != nil){
        
        NSString* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
        folderSize += [self fileSizeAtPath:fileAbsolutePath];
    }
    
    return folderSize/(1024.0*1024.0);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionCell *cell = [self.collection dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
    YZLDiscuss *discuss = [[YZLDiscuss alloc] init];
    
    if (0 == indexPath.item) {
        discuss.title = @"我的收藏";
        discuss.image = @"collection";
    } else if (1 == indexPath.item) {
        discuss.title = @"清理缓存";
        discuss.image = @"16";
    } else if (2 == indexPath.item) {
        discuss.title = @"关于我们";
        discuss.image = @"17";
    } else if (3 == indexPath.item) {
        discuss.title = @"热门活动";
        discuss.image = @"18";
    } else if (4 == indexPath.item) {
        discuss.title = @"保驾租车";
        discuss.image = @"20";
    } else if (5 == indexPath.item) {
        discuss.title = @"意见反馈";
        discuss.image = @"19";
    }
    cell.discuss = discuss;
    return cell;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatHead];
    [self creatCollection];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
